package com.itheima.sh.service;

import com.itheima.sh.dao.UserMapper;
import com.itheima.sh.pojo.User;
import com.itheima.sh.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl {
    public User login(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.login(user);
        sqlSession.close();
        return user1;
    }

    public boolean register(User user) {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            String username = user.getUsername();
            User user1 = mapper.selectUserByName(username);
            if (user1 == null){
                mapper.insertUser(user);
                return true;
            }else {
                return false;
            }
        } finally {
            sqlSession.close();
        }

    }
}
